package com.campus.controller;

import com.campus.domain.po.Student;
import com.campus.service.StudentService;
import com.campus.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;
    @PreAuthorize("hasAnyRole('ADMIN', 'ASSISTANT')")
    @PostMapping("/add")
    public R addStudent(@RequestBody Student student) {
        Student savedStudent = studentService.saveStudent(student);
        return R.success(savedStudent);
    }
    @PreAuthorize("hasAnyRole('ADMIN', 'ASSISTANT')")
    @DeleteMapping("/delete/{id}")
    public R deleteStudent(@PathVariable Long id) {
        boolean removed = studentService.deleteStudentById(id);
        return removed ? R.success(200) : R.error(500,"Failed to delete student");
    }
    @PreAuthorize("hasAnyRole('ADMIN', 'ASSISTANT')")
    @PutMapping("/update")
    public R updateStudent(@RequestBody Student student) {
        Student updatedStudent = studentService.updateStudent(student);
        return updatedStudent != null ? R.success(updatedStudent) : R.error(500,"Failed to update student");
    }
    @PreAuthorize("hasAnyRole('ADMIN', 'ASSISTANT')")
    @GetMapping("/get/{id}")
    public R getStudent(@PathVariable Long id) {
        Student student = studentService.getStudentById(id);
        return student != null ? R.success(student) : R.error(500,"Student not found");
    }
    @PreAuthorize("hasAnyRole('ADMIN', 'ASSISTANT')")
    @PostMapping("/import")
    public R importStudents(@RequestBody List<Student> students) {
        boolean result = studentService.importStudents(students);
        return result ? R.success("Batch import successful") : R.error(500, "Failed to import students");
    }
    @PreAuthorize("hasAnyRole('ADMIN', 'ASSISTANT')")
    @PutMapping("/audit/{id}")
    public R auditStudent(@PathVariable Long id, @RequestParam String status) {
        boolean result = studentService.auditStudent(id, status);
        return result ? R.success("Student audited successfully") : R.error(500, "Failed to audit student");
    }
    @PreAuthorize("hasAnyRole('ADMIN', 'ASSISTANT')")
    @PutMapping("/audit/batch")
    public R auditStudents(@RequestBody List<Long> studentIds, @RequestParam String status) {
        boolean result = studentService.auditStudents(studentIds, status);
        return result ? R.success("Students audited successfully") : R.error(500, "Failed to audit students");
    }
}
